package io.itit.ecp.admin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.common.enums.IMeterDeviceEnums;
import io.itit.ecp.admin.server.entity.MeterDeviceEntity;
import io.itit.ecp.admin.server.entity.extend.MeterDeviceExtEntity;
import io.itit.ecp.admin.server.mapper.MeterDeviceMapper;
import io.itit.ecp.admin.server.service.IMeterDeviceService;
import io.itit.grass.common.server.entity.BaseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：计量设备管理服务实现类
 * @Date：Create by 2023-11-30 18:51:39
 */
@Service
public class MeterDeviceServiceImpl extends JoinServiceImpl<MeterDeviceMapper, MeterDeviceEntity> implements IMeterDeviceService {

    @Override
    public List<MeterDeviceExtEntity> countMeterType() {
        return this.baseMapper.countMeterType();
    }

    @Override
    public boolean deleteMeterDevice(List<String> ids) {
        if (ids.isEmpty()) {
            return true;
        }
        return this.lambdaUpdate()
                .set(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag2.delFlag)
                .in(BaseEntity::getId, ids)
                .update();
    }

    @Override
    public MeterDeviceEntity findByDeviceCode(String deviceCode, String delFlag) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<>(MeterDeviceEntity.class)
                .eq(MeterDeviceEntity::getDeviceCode, deviceCode)
                .eq(StringUtils.hasLength(delFlag), MeterDeviceEntity::getDelFlag, delFlag)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveMeterDeviceBatch(List<MeterDeviceEntity> entities) {
        return this.saveBatch(entities);
    }

    @Override
    public int countOnline(String sceneProjectId, String sceneEdificeId, String sceneFloorId) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<MeterDeviceEntity>()
                        .eq(StringUtils.hasLength(sceneProjectId), MeterDeviceEntity::getSceneProjectId, sceneProjectId)
                        .eq(StringUtils.hasLength(sceneEdificeId), MeterDeviceEntity::getSceneEdificeId, sceneEdificeId)
                        .eq(StringUtils.hasLength(sceneFloorId), MeterDeviceEntity::getSceneFloorId, sceneFloorId)
                        .eq(MeterDeviceEntity::getDeviceStatus, "1")
                        .eq(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag1.delFlag))
                .intValue();
    }

    @Override
    public int countOffline(String sceneProjectId, String sceneEdificeId, String sceneFloorId) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<MeterDeviceEntity>()
                .eq(StringUtils.hasLength(sceneProjectId), MeterDeviceEntity::getSceneProjectId, sceneProjectId)
                .eq(StringUtils.hasLength(sceneEdificeId), MeterDeviceEntity::getSceneEdificeId, sceneEdificeId)
                .eq(StringUtils.hasLength(sceneFloorId), MeterDeviceEntity::getSceneFloorId, sceneFloorId)
                .eq(MeterDeviceEntity::getDeviceStatus, "2")
                .eq(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag1.delFlag)
        ).intValue();
    }
}
